package jinyilw.swing.rlaf.ui;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.plaf.basic.BasicRadioButtonUI;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

/**
 * @author Jinyi
 */
public class RRadioButtonUI extends BasicRadioButtonUI
{
	private static RRadioButtonUI radioButtonUI;

	public static ComponentUI createUI(JComponent c)
	{
		if (radioButtonUI == null)
		{
			radioButtonUI = new RRadioButtonUI();
		}
		return radioButtonUI;
	}

	@Override
	public void installDefaults(AbstractButton b)
	{
		super.installDefaults(b);
		b.setRolloverEnabled(true);
		b.setOpaque(false);
		b.setFont(FontTools.defaultFont);
	}

	@Override
	public void update(Graphics g, JComponent c)
	{
		Composite composite = ComponentTools.applyEnableAlpha((Graphics2D) g, c,
				0.92f, 0.3f);
		if (c.getClientProperty(BasicHTML.propertyKey) != null)
			RThemeColor.customForegroundSetting(c);
		paint(g, c);
		if (composite != null)
			((Graphics2D) g).setComposite(composite);
	}

	@Override
	protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
			String text)
	{
		FontMetrics fm = g.getFontMetrics();
		int mnemonicIndex = b.getDisplayedMnemonicIndex();
		Color fColor = b.getForeground();
		if (fColor == null || fColor instanceof UIResource)
			g.setColor(RThemeColor.colorSet.textColor);
		else
			g.setColor(fColor);
		GraphicsTools.drawStringUnderlineCharAt(g, text, mnemonicIndex,
				textRect.x + getTextShiftOffset(),
				textRect.y + fm.getAscent() + getTextShiftOffset());
	}
}
